package main

import (
	"github.com/go-kratos/kratos/v2/transport/grpc"
	"github.com/google/wire"

	v12 "gitee.com/shuokeyun/apis/item-service/api/v1"
	v11 "gitee.com/shuokeyun/apis/merchant-service/api/v1"
	v1 "gitee.com/shuokeyun/apis/order-service/api/v1"
	client "gitee.com/shuokeyun/kratos/client"
	"gitee.com/shuokeyun/kratos/server"
	v13 "gitee.com/shuokeyun/med-bff/api/v1"
	biz "gitee.com/shuokeyun/med-bff/internal/biz"
	service "gitee.com/shuokeyun/med-bff/internal/service"
)

var providerSet = wire.NewSet(
	newRegisterGRPCServer,
	service.NewOrderServer,
	biz.NewOrderUsecase,
	newOrderClient,
	newMerchantClient,
	newItemClient,
)

func newRegisterGRPCServer(orderServer v13.OrderServer) server.WithAfterGRPCServerFunc {
	return func(g *grpc.Server) error {
		v13.RegisterOrderServer(g, orderServer)
		return nil
	}
}

func newItemClient(app *server.App) (v12.ItemClient, error) {
	insecure, err := client.NewGrpcClientConn(app.GetDiscovery(), v12.Item_ServiceDesc.ServiceName, app.GetConfigBootstrap().GetServer().GetName())
	if err != nil {
		return nil, err
	}
	return v12.NewItemClient(insecure), nil
}

func newMerchantClient(app *server.App) (v11.MerchantClient, error) {
	insecure, err := client.NewGrpcClientConn(app.GetDiscovery(), v11.Merchant_ServiceDesc.ServiceName, app.GetConfigBootstrap().GetServer().GetName())
	if err != nil {
		return nil, err
	}
	return v11.NewMerchantClient(insecure), nil
}

func newOrderClient(app *server.App) (v1.OrderClient, error) {
	insecure, err := client.NewGrpcClientConn(app.GetDiscovery(), v1.Order_ServiceDesc.ServiceName, app.GetConfigBootstrap().GetServer().GetName())
	if err != nil {
		return nil, err
	}
	return v1.NewOrderClient(insecure), nil
}
